package com.celan.year2023.month07.day17;

/**
 * @author Celan
 * @description TODO
 */
public class Solution {
    public String addStrings(String num1, String num2) {
        int idx1 = num1.length() - 1, idx2 = num2.length() - 1;
        StringBuilder res = new StringBuilder();
        int carry = 0;

        while (idx1 >= 0 || idx2 >= 0) {
            int n1 = idx1 >= 0 ? num1.charAt(idx1) - '0' : 0;
            int n2 = idx2 >= 0 ? num2.charAt(idx2) - '0' : 0;
            int add = n1 + n2 + carry;
            if (add >= 10) {
                carry = 1;
                add -= 10;
            } else {
                carry = 0;
            }
            res.append(add);
            idx1--;
            idx2--;
        }
        if (carry == 1) res.append(1);

        return res.reverse().toString();
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        String s = solution.addStrings("11", "123");
        System.out.println(s);
    }
}
